<script>
import { mapMutations } from 'vuex';
import cache from './utils/cached.js'
import dayjs from 'dayjs';
import { getToken, setToken } from '@/utils/auth'

// #ifdef APP-PLUS
const prodToken = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjNhMTNlNzRiZWNkZjU2YTZlYjFkN2E0MjFhOGVjNzQ3IiwiZ2l2ZW5fbmFtZSI6ImxzeSIsInJvbGUiOiJBUFBVU0VSIiwiZXhwIjoyMzQzNjQyNTAyLCJqdGkiOiIzYTEzZTc0YmVkNjNlNzQwMmU5MDc1MTAyOTIzMmEyNiIsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDQzOTYiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjQ0Mzk2In0.NyMCckTmmwC6L2ejlOkMmsPPn3R1zl14dvEhFrRnSR8';
// #endif
// #ifdef H5
const prodToken = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjNhMTNlNzRiZWNkZjU2YTZlYjFkN2E0MjFhOGVjNzQ3IiwiZ2l2ZW5fbmFtZSI6ImxzeSIsInJvbGUiOiJBUFBVU0VSIiwiZXhwIjoyMzQzNjQyNTAyLCJqdGkiOiIzYTEzZTc0YmVkNjNlNzQwMmU5MDc1MTAyOTIzMmEyNiIsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDQzOTYiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjQ0Mzk2In0.NyMCckTmmwC6L2ejlOkMmsPPn3R1zl14dvEhFrRnSR8'
// #endif

export default {
	globalData: {
		// 运行环境
		appEnv: 'local',
		userName: '用户',
		deviceCode: '',
		initToken: ''
	},
	onLaunch: function(options) {
		this.init(options)
	},
	onHide: function() {
		console.log('App Hide')
	},
	methods: {
		init(params) {
			// 清空本地缓存
			cache.clearAllCache();
			// 保存全局数据
			this.setGlobalData(params);
			// 初始设置token
			// const token = params.token || prodToken;
			// this.setToken(token);
			
			this.setEnv(params.env);

			this.redirect()
		},
		setGlobalData(launchParams) {
			this.globalData.userName = launchParams.userName || '用户';
			this.globalData.deviceCode = launchParams.deviceCode || '';
			this.globalData.id = launchParams.id || '';
			this.globalData.date = launchParams.date || dayjs().format('YYYY-MM-DD');
			this.globalData.initToken = launchParams.token || prodToken;
		},
		setEnv(env) {
			if (env) {
				this.globalData.appEnv = env;
			} else if (process.env.NODE_ENV === 'development') {
				this.globalData.appEnv = 'local';
			} else {
				this.globalData.appEnv = 'dev';
			}
		},
		redirect() {
			if (!getToken()) {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			} else {
				uni.reLaunch({
					url: '/pages/home/home'
				})
			}
		}
	}
}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-ui/index.scss";
	@import './styles/common.scss';
</style>
